package com.example.perplonews.a_d_fragment.a_news

import BaseFragment
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.perplonews.R
import com.example.perplonews.a_a_mvp.contract.News_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_a_mvp.model.models.News_Model
import com.example.perplonews.a_a_mvp.presenter.presenters.News_Presenter
import com.example.perplonews.a_b_entity.BaseEntity
import com.example.perplonews.a_b_entity.ImageBannerEntity
import com.example.perplonews.a_b_entity.NewsEntity
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.a_news.adapter.ImageBannerAdapter
import com.example.perplonews.a_d_fragment.a_news.adapter.News_Adapter
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
import com.youth.banner.Banner
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.transformer.ZoomOutPageTransformer

class BlankFragment : BaseFragment<News_Presenter>, News_Contract.NewsView,
    OnBannerListener<ImageBannerAdapter>, BaseQuickAdapter.OnItemClickListener {
    private val fragPull: PullToRefreshLayout by lazy { findViewById<PullToRefreshLayout>(R.id.frag_pull) }
    private val fragRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.frag_rec) }
    private var user_id: Long? = null
    var adapter: ImageBannerAdapter? = null
    private var banner: Banner<ImageBannerEntity, ImageBannerAdapter>? = null

    //获取到id
    constructor(user_id: Long?) {
        this.user_id = user_id
    }


    override fun findlayout(): Int {
        return R.layout.fragment_blank
    }

    override fun initView() {

    }

    override fun initData() {
        mypre = News_Presenter(News_Model(), this)
        //请求数据
        var map: HashMap<String, Any> = HashMap()
        map.put("code", HttpCode.IMAGERBANNERCODE)

        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        list.add(map)

        mypre.requestAll(list)
        fragPull.setRefreshListener(object : BaseRefreshListener {
            override fun refresh() {
                mypre.requestAll(list)
                fragPull.finishRefresh()

            }

            override fun loadMore() {
                mypre.requestAll(list)
                fragPull.finishLoadMore()
            }

        })
    }


    override fun refreshAll(entity: BaseEntity) {



        if (entity is ImageBannerEntity) {
            //获取banner布局
            var inflate = LayoutInflater.from(activity).inflate(R.layout.newsbanner, null)
            banner =
                inflate.findViewById<Banner<ImageBannerEntity, ImageBannerAdapter>>(R.id.banner_banner)

            var values = entity.getValues()



            adapter = ImageBannerAdapter(values as ArrayList<NewsEntity.Values>)

            banner?.adapter = adapter
            banner?.setOrientation(Banner.HORIZONTAL)
                ?.setPageTransformer(ZoomOutPageTransformer())

                ?.start()

            var newsAdapter: News_Adapter = News_Adapter(values as ArrayList<NewsEntity.Values>)
            fragRec.adapter = newsAdapter
            fragRec.layoutManager = LinearLayoutManager(context)
            newsAdapter.addHeaderView(inflate)
            newsAdapter.onItemClickListener = this

        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

        LjlUtils.Ljl_Loge("--------")
        var values = adapter.getItem(position) as NewsEntity.Values
        var newsId = values.news_id
        var news_url = values.news_url

        var intent = Intent(activity,News_Value_Activity::class.java)
        intent.putExtra("news_url", news_url)
        intent.putExtra("newsId", newsId)
        activity?.startActivity(intent)

    }

    override fun startPage(bundle: Bundle, clazz: Class<Any>) {

    }

    override fun OnBannerClick(data: ImageBannerAdapter?, position: Int) {
    }

    override fun onStart() {
        super.onStart()
        banner?.start()
    }

    override fun onStop() {
        super.onStop()
        banner?.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        banner?.destroy()
    }


}